//
//  LZFSettingVC.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/6/23.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFSettingVC.h"
#import "LZFThirdCell.h"
#import "LZFClearCell.h"
#import "LZFJumpVC.h"
#import "LZFFileTool.h"

#define cachePath NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]

@interface LZFSettingVC ()

@end

@implementation LZFSettingVC


static NSString *ID3=@"third";
static NSString *ID2=@"normal";
static NSString *ID1=@"other";
- (void)viewDidLoad {
    [super viewDidLoad];
    
 
     //设置tableview
    [self setUpTableView];
    
    //设置导航条
    [self setUpNavigationBar];
    

    
    
}


-(void)setUpNavigationBar
{
    
    //设置标题
        self.title=@"设置";
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"jump" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
}

-(void)click
{
    LZFJumpVC *jumpVC=[[LZFJumpVC alloc]init];
    [self.navigationController pushViewController:jumpVC animated:YES];
    
}

 //设置tableview
-(void)setUpTableView
{
    //注册cell
    self.tableView.contentInset=UIEdgeInsetsMake(-20, 0, 0, 0);
    self.tableView.backgroundColor=LZFColor(215, 215, 215);
    
    
    //清除缓存cell
    [self.tableView registerClass:[LZFClearCell class] forCellReuseIdentifier:ID1];
    
    //普通cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID2];
    
    
    //第三种cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LZFThirdCell class]) bundle:nil] forCellReuseIdentifier:ID3];
    
}


-(instancetype)initWithStyle:(UITableViewStyle)style
{
   return  [super initWithStyle:UITableViewStyleGrouped];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  5;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0&&indexPath.row==0) {
        LZFClearCell *cell=[tableView dequeueReusableCellWithIdentifier:ID1];
        cell.textLabel.text=[self getFileText:cachePath];
        //显示当前的缓存
        
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
   else if (indexPath.section==1&&indexPath.row==2)
   {
       LZFThirdCell *thirdCell=[tableView dequeueReusableCellWithIdentifier:ID3];
       return  thirdCell;
   }
    
    else
    {
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID2];
        cell.textLabel.text=[NSString stringWithFormat:@"%ld--%ld",indexPath.section,indexPath.row];
        return cell;
    }
    
}

//得到缓存文字
-(NSString *)getFileText:(NSString *)cacheDirectoryPath
{
    return  [self resolveFileCacheText:[LZFFileTool getFileCache:cacheDirectoryPath]];
}


//点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0&&indexPath.row==0) {
        
        if ([LZFFileTool getFileCache:cachePath]>0) {
        
        //点击了清除缓存cell
            [LZFFileTool removeFileCache:cachePath];
        
        [self.tableView reloadData];
        
       }
    }

}

//处理cache大小,返回转换的text
-(NSString *)resolveFileCacheText:(NSInteger)totalSize
{
    
    //1M=1000KB=1000000B
    if (totalSize>10000) {
        return [NSString stringWithFormat:@"%0.1fMB",totalSize/1000000.0];
    }
    
    else if (totalSize>1000) {
        return [NSString stringWithFormat:@"%0.1fKB",totalSize/1000.0];
    }
    
    else if(totalSize >0)
    {
        return [NSString stringWithFormat:@"%ldB",totalSize];
    }
    
    else
    {
        return @"清除缓存";
    }
    
    
}



/*
//清除缓存
-(void)removeFileCache:(NSString *)cacheDirectoryPath
{
        
        NSFileManager *mgr=[NSFileManager defaultManager];
        
        NSArray *contentArray = [mgr contentsOfDirectoryAtPath:cacheDirectoryPath error:nil];
        
        for (NSString *contentPath in contentArray) {
            NSString *fullPath =  [cacheDirectoryPath stringByAppendingPathComponent:contentPath];
            
            [mgr removeItemAtPath:fullPath error:nil];
        }
   

}
*/

/*
//获得缓存大小,cache文件夹的总大小
-(NSInteger)getFileCache:(NSString *)cacheDirectoryPath
{
    
    NSFileManager *mgr=[NSFileManager defaultManager];
    
     NSArray *subsArray =  [mgr subpathsOfDirectoryAtPath:cacheDirectoryPath error:nil];
    
    NSInteger totalSise =0;
    for (NSString *subPaths in subsArray) {
      NSString *fullPaths =   [cacheDirectoryPath stringByAppendingPathComponent:subPaths];
        
        BOOL isDirectory;
        BOOL isExist =   [mgr fileExistsAtPath:fullPaths isDirectory:&isDirectory];
        
        //文件过滤(.DS文件过滤掉,文件夹过滤掉,不是文件也是过滤掉)
        if ([fullPaths containsString:@".DS"])continue;
        if (!isExist || isDirectory) continue;
        
        NSDictionary *attDic= [mgr attributesOfItemAtPath:fullPaths error:nil];
        
        totalSise=totalSise+[attDic fileSize];


    }
   
    NSLog(@"%ld",totalSise);
    return  totalSise;

}
 
 */











@end
